using ASP;
using HoWave.Common;
using HoWave.Web;
using HoWave.Web.Shop;
using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Web.Profile;
using System.Web.SessionState;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
public class Passport_forget_password : Shop, IRequiresSessionState
{
	protected HtmlHead Head1;
	protected UserControls_cmpy_Top top1;
	protected RequiredFieldValidator err1;
	protected Label login_err_show;
	protected TextBox UserName;
	protected LinkButton forgetPasswordSubmit;
	protected Panel Step1;
	protected RequiredFieldValidator RequiredFieldValidator1;
	protected Label answer_err;
	protected Literal Question;
	protected TextBox Answer;
	protected TextBox UserEmail;
	protected LinkButton ConfirmAnswerBtn;
	protected Panel Step2;
	protected Label pwd_err;
	protected TextBox userSetNewPassword;
	protected RegularExpressionValidator RegularExpressionValidator1;
	protected TextBox userSetNewPassword1;
	protected CompareValidator CompareValidator1;
	protected LinkButton ConfirmNewPasswrodBtn;
	protected Panel Step3;
	protected UserControls_CMYP_Foot Foot1;
	protected HtmlForm form1;
	private UserBll _user;
	private string ComeUrl = "";
	private string m_Other;
	protected DefaultProfile Profile
	{
		get
		{
			return (DefaultProfile)this.Context.Profile;
		}
	}
	protected global_asax ApplicationInstance
	{
		get
		{
			return (global_asax)this.Context.ApplicationInstance;
		}
	}
	protected UserBll m_User
	{
		get
		{
			UserBll user;
			if (this._user == null)
			{
				this._user = new UserBll(this.conn);
				user = this._user;
			}
			else
			{
				user = this._user;
			}
			return user;
		}
	}
	protected void Page_Init(object sender, EventArgs e)
	{
		this.SelectAction();
		try
		{
		}
		catch (Exception ex)
		{
			base.WriteTemErrMsg(ex.Message.ToString());
		}
	}
	protected void SelectAction()
	{
		this.ComeUrl = Text.GetComeUrl(Fetch.Get("ComeUrl"));
		if (this.ComeUrl == string.Empty)
		{
			this.ComeUrl = this.config["SiteUrl"].ToString();
		}
		if (Fetch.Get("other") != string.Empty)
		{
			this.m_Other = Fetch.Get("other");
		}
		if (Fetch.Get("CheckNum") != string.Empty && Fetch.Get("UserName") != string.Empty)
		{
			this.EmailGetPWD();
		}
		if (this.isNext1Post())
		{
			this.ForgetPasswordNext1();
		}
		if (this.isNext2Post())
		{
			this.ForgetPasswordNext2();
		}
		if (this.isNext3Post())
		{
			this.ForgetPasswordNext3();
		}
	}
	protected bool isNext1Post()
	{
		return Fetch.Post("UserName") != string.Empty;
	}
	protected bool isNext2Post()
	{
		return Fetch.Post("Answer") != string.Empty && Fetch.Post("UserEmail") != string.Empty;
	}
	protected bool isNext3Post()
	{
		return Fetch.Post("userSetNewPassword") != string.Empty && Fetch.Post("userSetNewPassword1") != string.Empty;
	}
	protected void Page_Load(object sender, EventArgs e)
	{
	}
	protected void EmailGetPWD()
	{
		string text = Fetch.Get("CheckNum");
		string userName = Fetch.Get("UserName");
		DataTable userByUserName = this.m_User.GetUserByUserName(userName);
		if (userByUserName.Rows.Count > 0)
		{
			if (userByUserName.Rows[0]["CheckNum"].ToString() == text && userByUserName.Rows[0]["CheckNum"].ToString() != null && userByUserName.Rows[0]["CheckNum"].ToString() != "")
			{
				if (this.m_User.ActiveAccount(userByUserName.Rows[0]["UserName"].ToString(), text, ""))
				{
					this.Session["GetPasswordUserID"] = userByUserName.Rows[0]["UserID"].ToString();
					this.Step3.Visible = true;
					this.Step1.Visible = false;
					this.Step2.Visible = false;
				}
			}
		}
	}
	protected void ForgetPasswordNext1()
	{
		string userName = Fetch.Post("UserName");
		DataTable userByUserName = this.m_User.GetUserByUserName(userName);
		if (userByUserName.Rows.Count > 0)
		{
			this.Step1.Visible = false;
			this.Step2.Visible = true;
			this.Step3.Visible = false;
			this.Question.Text = userByUserName.Rows[0]["Question"].ToString();
			this.Session["GetPasswordUserName"] = userByUserName.Rows[0]["UserName"].ToString();
		}
		else
		{
			this.WriteErrMsg("", Language.Get("PassPort/NoUserName"), "");
		}
	}
	protected void ForgetPasswordNext2()
	{
		if (this.Session["GetPasswordUserName"] == null)
		{
			this.Step1.Visible = true;
			this.Step2.Visible = false;
			this.Step3.Visible = false;
		}
		else
		{
			DataTable userByUserName = this.m_User.GetUserByUserName(Convert.ToString(this.Session["GetPasswordUserName"]));
			if (userByUserName.Rows.Count != 1)
			{
				this.Step1.Visible = true;
				this.Step2.Visible = false;
				this.Step3.Visible = false;
				this.Session["GetPasswordUserName"] = null;
			}
			else
			{
				int db_userid = Convert.ToInt32(userByUserName.Rows[0]["UserID"].ToString());
				string text = userByUserName.Rows[0]["Email"].ToString();
				string str = userByUserName.Rows[0]["Answer"].ToString();
				if (!Text.CheckMd5Text(str, this.Md5(Fetch.Post("Answer"), 32)))
				{
					this.WriteErrMsg("", Language.Get("PassPort/AnswerErr"), "");
				}
				if (text != Fetch.Post("UserEmail"))
				{
					this.WriteErrMsg("", Language.Get("PassPort/EmailNotRight"), "");
				}
				userByUserName.Dispose();
				string a = this.config["WebLiveSet"].ToString();
				if (a == "0")
				{
					this.SendEmailFindPassword(db_userid, text);
				}
				else
				{
					if (a == "1")
					{
						this.Session["GetPasswordUserID"] = db_userid.ToString();
						this.Step3.Visible = true;
						this.Step1.Visible = false;
						this.Step2.Visible = false;
						this.Step2.EnableViewState = false;
					}
				}
			}
		}
	}
	protected void ForgetPasswordNext3()
	{
		string text = Fetch.Post("userSetNewPassword").Trim();
		string b = Fetch.Post("userSetNewPassword1").Trim();
		if (text != b)
		{
			this.WriteErrMsg("", Language.Get("PassPort/rePasswordErr"), "");
		}
		if (text.Length < 2)
		{
			this.WriteErrMsg("", Language.Get("PassPort/PasswordLengErr"), "");
		}
		if (this.checkpassword(text) == 0 && this.checkpassword(text) != -1)
		{
			this.WriteErrMsg("", "密码长度不对", "");
		}
		else
		{
			if (this.checkpassword(text) == 1 && this.checkpassword(text) != -1)
			{
				this.WriteErrMsg("", "密码不能全部是字符", "");
			}
			else
			{
				if (this.checkpassword(text) == 2 && this.checkpassword(text) != -1)
				{
					this.WriteErrMsg("", "密码不能全部是数字", "");
				}
				else
				{
					if (this.checkpassword(text) == 3 && this.checkpassword(text) != -1)
					{
						this.WriteErrMsg("", "您设置的密码有误，不能设置连续相同的字符或数字", "");
					}
					else
					{
						if (this.checkpassword(text) == 4 || this.checkpassword(text) == 5 || this.checkpassword(text) == 6)
						{
							this.WriteErrMsg("", "您设置的密码有误，密码应该由6-16个英文字母、数字或符号组成!", "");
						}
						else
						{
							if (this.Session["GetPasswordUserID"] == null)
							{
								this.Step1.Visible = false;
								this.Step2.Visible = true;
								this.Step3.Visible = false;
							}
							else
							{
								DataTable userByUserID = this.m_User.GetUserByUserID(Convert.ToInt32(this.Session["GetPasswordUserID"]));
								if (userByUserID.Rows.Count <= 0)
								{
									this.Step1.Visible = false;
									this.Step2.Visible = true;
									this.Step3.Visible = false;
									this.Session["GetPasswordUserID"] = null;
								}
								else
								{
									string text2 = userByUserID.Rows[0]["Password"].ToString();
									int userID = Convert.ToInt32(userByUserID.Rows[0]["UserID"].ToString());
									string userName = userByUserID.Rows[0]["UserName"].ToString();
									string text3 = this.Md5(text, 32);
									if (this.m_User.UpdatePassport(userID, text3, "", "", ""))
									{
										this.Session["GetPasswordUserID"] = null;
										this.Session["GetPasswordUserName"] = null;
										if (this.config["HLIntegration"].ToString() == "1")
										{
											new Integrate(this.conn)
											{
												ComeUrl = this.ComeUrl,
												sendEnd = "<script>alert('" + Language.Get("PassPort/EditPWDSuccess") + "');top.location.href='{#GoUrl#}';</script>"
											}.Send("Modify", userName, text3.ToLower(), "", "", "", 0, text2.ToLower(), this.m_Other);
										}
										else
										{
											if (this.ComeUrl == string.Empty)
											{
												this.ComeUrl = this.config["SiteUrl"].ToString();
											}
											this.WriteSuccessMsg("", Language.Get("PassPort/EditPWDSuccess"), this.ComeUrl);
										}
									}
									else
									{
										this.Session["GetPasswordUserID"] = null;
										this.Session["GetPasswordUserName"] = null;
										this.WriteErrMsg("", Language.Get("Common/Fail", ""), "");
									}
								}
							}
						}
					}
				}
			}
		}
	}
	public int checkpassword(string password)
	{
		Regex regex = new Regex("^[A-Za-z]+$");
		int result;
		if (regex.IsMatch(password))
		{
			result = 1;
		}
		else
		{
			if (Regex.IsMatch(password, "^([+-]?)\\d*$"))
			{
				result = 2;
			}
			else
			{
				char[] array = password.ToCharArray();
				if (password.Length < 6 || password.Length > 16)
				{
					result = 0;
				}
				else
				{
					int i;
					for (i = 0; i < password.Length; i++)
					{
						if (array[0] != array[i])
						{
							break;
						}
					}
					if (i == array.Length)
					{
						result = 3;
					}
					else
					{
						string text = "01234567890";
						if (text.IndexOf(password) != -1)
						{
							result = 4;
						}
						else
						{
							string text2 = "abcdefghijklmnopqrstuvwxyz";
							if (text2.IndexOf(password) != -1)
							{
								result = 5;
							}
							else
							{
								string text3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
								if (text3.IndexOf(password) != -1)
								{
									result = 6;
								}
								else
								{
									result = -1;
								}
							}
						}
					}
				}
			}
		}
		return result;
	}
	protected void SendEmailFindPassword(int db_userid, string db_email)
	{
		string smtpserver = this.config["MailSmtpServer"].ToString();
		string mailuser = this.config["MailServerUserName"].ToString();
		string mailpassword = this.config["MailServerPassWord"].ToString();
		string mailFrom = this.config["MailServerFrom"].ToString();
		string text = this.config["MessageOfConfrimPassword"].ToString();
		string text2 = this.MakePassword();
		string mailTitle = Language.Get("PassPort/GetPWDEmailTitle", new string[]
		{
			this.config["SiteName"].ToString(),
			this.Session["GetPasswordUserName"].ToString()
		});
		string text3 = text.Replace("{$CheckNum$}", text2);
		text3 = text3.Replace("{$UserName$}", this.Session["GetPasswordUserName"].ToString());
		text3 = text3.Replace("{$SiteName$}", this.config["SiteName"].ToString());
		text3 = text3.Replace("{$SiteUrl$}", this.config["SiteUrl"].ToString());
		text3 = text3.Replace("{$InputTime$}", DateTime.Now.ToString());
		this.conn.Execute(string.Concat(new object[]
		{
			"Update [HL_User] Set CheckNum='",
			text2,
			"' Where UserID=",
			db_userid
		}));
		Email email = new Email();
		bool flag = email.SendEmail(smtpserver, mailuser, mailpassword, mailFrom, text3, db_email, mailTitle);
		this.Session["GetPasswordUserName"] = null;
		if (flag)
		{
			if (this.ComeUrl == string.Empty)
			{
				this.ComeUrl = this.config["SiteUrl"].ToString();
			}
			this.WriteSuccessMsg("", Language.Get("PassPort/GetPasswrodSuccess"), this.ComeUrl);
		}
		else
		{
			this.WriteErrMsg("", "邮件发送失败！", "");
		}
	}
}
